package com.pioneer.controller.purchase;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.purchase.domain.PurPlan;
import com.pioneer.web.purchase.service.IPurPlanService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 采购计划
 *
 * @author hlm
 */
@RestController
@RequestMapping("/purchase/plan")
public class PurPlanController extends BaseController {

    @Resource
    private IPurPlanService planService;

    /**
     * 获取采购计划列表
     *
     * @param plan 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('purchase:plan:list')")
    @GetMapping("/list")
    public AjaxResult list(PurPlan plan) {
        boolean isPage = startPage();
        List<PurPlan> list = planService.list(Wrappers.query(plan));
        return success(isPage, list);
    }

    /**
     * 根据采购计划id获取详细信息
     *
     * @param planId 采购计划id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('purchase:plan:query')")
    @GetMapping(value = "/{planId}")
    public AjaxResult getInfo(@PathVariable Long planId) {
        return AjaxResult.success(planService.selectPlanById(planId));
    }

    /**
     * 新增采购计划
     *
     * @param plan 采购计划
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('purchase:plan:add')")
    @Log(title = "采购计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PurPlan plan) {
        if (CollUtil.isEmpty(plan.getPlanBodyList())) {
            return AjaxResult.error("采购计划子信息不能为空");
        }
        return toAjax(planService.addPlan(plan));
    }

    /**
     * 修改采购计划
     *
     * @param plan 采购计划
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('purchase:plan:edit')")
    @Log(title = "采购计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PurPlan plan) {
        if (CollUtil.isEmpty(plan.getPlanBodyList())) {
            return AjaxResult.error("采购计划子信息不能为空");
        }
        return toAjax(planService.editPlan(plan));
    }

    /**
     * 删除采购计划
     *
     * @param planIds 采购计划id数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('purchase:plan:remove')")
    @Log(title = "采购计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{planIds}")
    public AjaxResult remove(@PathVariable Long[] planIds) {
        return toAjax(planService.deletePlans(Arrays.asList(planIds)));
    }

    /**
     * 提交采购计划
     * @param plan 采购计划
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('purchase:plan:commit')")
    @Log(title = "采购计划", businessType = BusinessType.OTHER)
    @PostMapping("/commit")
    public AjaxResult commit(@RequestBody PurPlan plan) {
        return toAjax(planService.commitPlan(plan));
    }

    /**
     * 撤回采购计划
     * @param plan 采购计划
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('purchase:plan:recall')")
    @Log(title = "采购计划", businessType = BusinessType.OTHER)
    @PostMapping("/recall")
    public AjaxResult recall(@RequestBody PurPlan plan) {
        return toAjax(planService.recallPlan(plan));
    }

    /**
     * 审批采购计划
     * @param plan 采购计划
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('purchase:plan:approve')")
    @Log(title = "采购计划", businessType = BusinessType.OTHER)
    @PostMapping("/approve")
    public AjaxResult approve(@RequestBody PurPlan plan) {
        return toAjax(planService.approvePlan(plan));
    }

    /**
     * 弃审采购计划
     * @param plan 采购计划
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('purchase:plan:unapprove')")
    @Log(title = "采购计划", businessType = BusinessType.OTHER)
    @PostMapping("/unapprove")
    public AjaxResult unapprove(@RequestBody PurPlan plan) {
        return toAjax(planService.unapprovePlan(plan));
    }
}
